<template>
  <el-container class="contect">
    <!-- 头部 -->
    <el-header>
      <div>
        <i class="iconfont icon-logo_apple"></i>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="logut"> 退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="flagButton ? '64px' : '200px'">
        <div class="toggle_button" @click="collapseButton">|||</div>

        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="flagButton"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in meunList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconFonts[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--  二级菜单-->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveActive('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 名字 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //是否开启折叠动画
      flagButton: false,
      //当前激活的菜单
      activePath: "",
      meunList: [],
      iconFonts: {
        "125": "iconfont icon-yonghu",
        "103": "iconfont icon-quanxianshenpi",
        "101": "iconfont icon-shangpingouwudai2",
        "102": "iconfont icon-ic_salesadvice",
        "145": "iconfont icon-shuju"
      },

    }
  }
  ,
  created() {
    this.getmeunList()
    this.activePath = window.sessionStorage.getItem("activePath")
  }
  ,
  methods: {
    logut() {
      window.sessionStorage.clear()
      this.$router.push("/login")
    },
    async getmeunList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message({ showClose: true, message: res.meta.message, type: 'error' })
      this.meunList = res.data
      // console.log(this.meunList)

    },
    collapseButton() {
      // console.log("111")
      this.flagButton = !this.flagButton
    },
    saveActive(pathActive) {

      window.sessionStorage.setItem("activePath", pathActive)
      this.activePath = pathActive


    }
  }
}
</script>

<style lang="less" scoped>
.contect {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;

    span {
      margin-left: 15px;
      font-size: 16px;
      color: rgba(255, 255, 255, 0.788);
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle_button {
  line-height: 24px;
  background-color: #4a5064;
  text-align: center;
  font-size: 10px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>